package opd.servlets;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import opd.db.services.ITargetsDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import com.google.gson.Gson;

/**
 * Servlet implementation class Targets
 */
@WebServlet("/Targets")
public class Targets extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	// ----------------------------------------------------------------------------------------------------
	
	@Autowired
	private ITargetsDao targetsDao;

	// ----------------------------------------------------------------------------------------------------
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Targets() {
        super();
        // TODO Auto-generated constructor stub
    }

    // ----------------------------------------------------------------------------------------------------
	
    @Override
    public void init(ServletConfig config) throws ServletException {
		super.init(config);
		
	    SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());		
    }
    
    // ----------------------------------------------------------------------------------------------------
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Gson gson = new Gson();
		response.getWriter().write(String.format("<div><p>Targets</p><p>%s</p></div>", gson.toJson(targetsDao.getLastTargets())));
	}
	
	// ----------------------------------------------------------------------------------------------------
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	// ----------------------------------------------------------------------------------------------------
	
	public ITargetsDao getTargetsDao() {
		return targetsDao;
	}
	
	// ----------------------------------------------------------------------------------------------------
	
	public void setTargetsDao(ITargetsDao targetsDao) {
		this.targetsDao = targetsDao;
	}	

}
